package commonfunctions;

import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * I wrote this class so that a file can be selected.
 * 
 * @author Trevor Terris
 *
 */
public class ChooseFile {

	/**
	 * The string s is the message that is displayed to the user, and a scanner over the 
	 * selected file is returned.
	 * 
	 * @param s
	 * @return
	 */
	public static Scanner select(String s) {
		do {
			JOptionPane.showMessageDialog(null, s, "",
					JOptionPane.INFORMATION_MESSAGE);
			JFileChooser chooser = new JFileChooser();
			int returnVal = chooser.showOpenDialog(null);
			try {
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					return new Scanner(chooser.getSelectedFile());
				} else {
					return null;
				}
			} catch (FileNotFoundException e) {
				JOptionPane.showMessageDialog(null, "Invalid file!", "error",
						JOptionPane.ERROR_MESSAGE);
			}
		} while (true);
	}
}
